package com.own.business.question.controller;

import com.own.aop.permission.PermissionModule;
import com.own.aop.permission.PermissionOperation;
import com.own.business.question.business.service.QuestionService;
import com.own.business.question.common.constant.ConstantModule;
import com.own.business.question.entity.dto.QuestionDto;
import com.own.business.question.entity.map.QuestionMapVo;
import com.own.business.question.entity.query.QuestionQuery;
import com.own.business.question.entity.vo.QuestionVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.store.core.confirm.aop.ConfirmAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import com.own.constant.ConstantPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * QuestionController
 *
 * @author chenxueli
 * @date 2021-06-18 11:52:00
 */
@Slf4j
@Tag(name = "QuestionController", description = "题目" + ConstantBeanSuffix.CONTROLLER)
@RestController
@PermissionModule(value = ConstantModule.QUESTION)
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "question")
public class QuestionController extends BaseController {

    @Resource
    private QuestionService questionService;

    @PermissionOperation(value = ConstantPermission.SEARCH)
    @Operation(summary = "分页查询信息")
    @PostMapping("/page")
    public ResultModel<PageModel<QuestionVo>> page(
            @RequestBody QuestionQuery query
    ) {
        var pageModel = questionService.page(query);
        return R.success(pageModel);
    }

    @Operation(summary = "map查询信息")
    @PostMapping("/map")
    public ResultModel<List<QuestionMapVo>> map(
            @RequestBody QuestionQuery query
    ) {
        var list = questionService.map(query);
        return R.success(list);
    }

    @PermissionOperation(value = ConstantPermission.ADD)
    @Operation(summary = "添加信息")
    @PostMapping("/")
    public ResultModel<QuestionVo> add(
            @Validated @RequestBody QuestionDto dto
    ) {
        dto.check();
        var updateModel = questionService.add(dto.toBo());
        return R.success(updateModel);
    }

    @PermissionOperation(value = ConstantPermission.UPDATE)
    @Operation(summary = "修改信息")
    @PutMapping("/{questionId}")
    public ResultModel<QuestionVo> update(
            @Parameter(description = "主键id", required = true) @PathVariable Long questionId,
            @Validated @RequestBody QuestionDto dto
    ) {
        dto.check();
        var updateModel = questionService.update(questionId, dto.toBo());
        return R.success(updateModel);
    }

    @PermissionOperation(value = ConstantPermission.DELETE)
    @ConfirmAop(message = "确认删除该题目？（注意：删除之后无法找回数据）")
    @Operation(summary = "删除信息")
    @DeleteMapping("/{questionId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long questionId
    ) {
        var updateModel = questionService.delete(questionId);
        return R.success(updateModel);
    }

    @Operation(summary = "根据id获取信息")
    @GetMapping("/{questionId}")
    public ResultModel<QuestionVo> getById(
            @Parameter(description = "主键id", required = true) @PathVariable Long questionId
    ) {
        var vo = questionService.getById(questionId);
        return R.success(vo);
    }

}
